package conf

import (
	"fmt"
	"github.com/micro/go-micro/v2/logger"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"micro-grpc-server/library/encrypt"
	"os"
	"sync"
)

var Instance *Config

type Config struct {
	App     Application           `yaml:"App"`
	Encrypt encrypt.Configuration `yaml:"Encrypt"`
}

type Application struct {
	Env         string `yaml:"Env"`
	Debug       bool   `yaml:"Debug"`
	LogLevel    string `yaml:"LogLevel"`
	LogFile     string `yaml:"LogFile"`
	CronLogFile string `yaml:"CronLogFile"`
	Port        int    `yaml:"Port"`
	Host        string `yaml:"Host"`
}

func init() {
	var once sync.Once
	once.Do(initConfig)
}

func initConfig() {
	Instance = &Config{}
	var env = os.Getenv("GO_SERVER_ENV")
	if len(env) == 0 {
		env = "dev"
	}
	if yamlFile, err := ioutil.ReadFile(fmt.Sprintf("config/app/application.%s.yaml", env)); err != nil {
		logger.Fatal(err)
	} else if err = yaml.Unmarshal(yamlFile, Instance); err != nil {
		logger.Fatal(err)
	}
}
